/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeRecording.tables;

import TimeRecording.business.DateRange;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author dpatchett
 */
@Entity
@Table(name = "session")
@NamedQueries({
    @NamedQuery(
            name = "Session.findAll",
            query = "SELECT s " +
                    "FROM Session s"),
    @NamedQuery(
            name = "Session.findBySessionId",
            query = "SELECT s " +
                    "FROM Session s " +
                    "WHERE s.sessionId = :sessionId"),
    @NamedQuery(
            name = "Session.findByStartTime",
            query = "SELECT s " +
                    "FROM Session s " +
                    "WHERE s.startTime = :startTime"),
    @NamedQuery(
            name = "Session.findByEndTime",
            query = "SELECT s " +
                    "FROM Session s " +
                    "WHERE s.endTime = :endTime"),
    @NamedQuery(
            name = "Session.findByHours",
            query = "SELECT s " +
                    "FROM Session s " +
                    "WHERE s.hours = :hours"),
    @NamedQuery(
            name = "Session.findByDescription",
            query = "SELECT s " +
                    "FROM Session s " +
                    "WHERE s.description = :description"),
    @NamedQuery(
            name = "Session.findByEmployeeAndDateRange",
            query = "SELECT s " +
                    "FROM Session s " +
                    "WHERE (s.employeeId = :employeeId) AND " +
                        "(s.startTime BETWEEN :lowerDate AND :upperDate)")})

public class Session implements Serializable {

    public final static String BASE_QUERY = "SELECT s FROM Session s";

    public final static String EMPLOYEE_FILTER = "s.employeeId = :employeeId";
    public final static String DATE_FILTER = "(s.startTime BETWEEN :lowerDate AND :upperDate)";

    public final static String COL_EMPLOYEE_ID = "employeeId";
    public final static String COL_LOWER_DATE = "lowerDate";
    public final static String COL_UPPER_DATE = "upperDate";

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "session_id")    
    private Integer sessionId;

    @Basic(optional = false)
    @Column(name = "start_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date startTime;

    @Column(name = "end_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date endTime;

    @Column(name = "hours")
    private Double hours;

    @Column(name = "description")
    private String description;

    @JoinColumn(name = "employee_id", referencedColumnName = "employee_id")
    @ManyToOne
    private Employee employeeId;

    // <editor-fold defaultstate="collapsed" desc="Property Change Support">
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
	changeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
	changeSupport.removePropertyChangeListener(listener);
    } // </editor-fold>

    public Session() {
    }

    public Session(Integer sessionId) {
        this.sessionId = sessionId;
    }

    public Session(Integer sessionId, Date startTime) {
        this.sessionId = sessionId;
        this.startTime = startTime;
    }

    public Integer getSessionId() {
        return sessionId;
    }

    public void setSessionId(Integer sessionId) {
        Integer oldSessionId = this.sessionId;
        this.sessionId = sessionId;
        changeSupport.firePropertyChange("sessionId", oldSessionId, sessionId);
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        Date oldStartTime = this.startTime;
        this.startTime = startTime;
        changeSupport.firePropertyChange("startTime", oldStartTime, startTime);
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        Date oldEndTime = this.endTime;
        this.endTime = endTime;
        changeSupport.firePropertyChange("endTime", oldEndTime, endTime);
    }

    public Double getHours() {
        return hours;
    }

    public void setHours(Double hours) {
        Double oldHours = this.hours;
        this.hours = hours;
        changeSupport.firePropertyChange("hours", oldHours, hours);
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        String oldDescription = this.description;
        this.description = description;
        changeSupport.firePropertyChange("description", oldDescription, description);
    }

    public Employee getEmployeeId() {
        return employeeId;
    }

    public void setEmployeeId(Employee employeeId) {
        Employee oldEmployeeId = this.employeeId;
        this.employeeId = employeeId;
        changeSupport.firePropertyChange("employeeId", oldEmployeeId, employeeId);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (sessionId != null ? sessionId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Session)) {
            return false;
        }
        Session other = (Session) object;
        if ((this.sessionId == null && other.sessionId != null) || (this.sessionId != null && !this.sessionId.equals(other.sessionId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "javaapplication7.Session[sessionId=" + sessionId + "]";
    }

    public void copy(Session session) {

        this.setSessionId(session.getSessionId());
        this.setStartTime(session.getStartTime());
        this.setEndTime(session.getEndTime());
        this.setHours(session.getHours());
        this.setDescription(session.getDescription());
        this.setEmployeeId(session.getEmployeeId());
    }

    public static Collection<Session> getSessionsForEmployeeAndDateRange(
            EntityManager entityManager, Employee employee, DateRange dateRange) {

        return entityManager.createNamedQuery("Session.findByEmployeeAndDateRange").
                setParameter("employeeId", employee).
                setParameter("lowerDate", dateRange.getFromDate()).
                setParameter("upperDate", dateRange.getToDate()).getResultList();
    }
}
